package com.android.clinic.app.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.clinic.app.utils.Utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by cheng on 2018/2/5.
 */
public abstract class BaseListAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<T> mData = new ArrayList<>();
    private LayoutInflater mInflater;

    private int resources[];

    private BaseListAdapter.OnItemClickListener mClickListener;
    private BaseListAdapter.OnItemLongClickListener mLongClickListener;

    public BaseListAdapter(Context context, int... resource) {
        this.resources = resource;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int typeIndex = getItemViewType(viewType);
        View convertView = mInflater.inflate(resources[typeIndex], parent, false);
        return (BaseViewHolder) setupViewHolder(convertView, parent, viewType);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        int typeIndex = getItemViewType(position);
        T object = mData.get(position);
        if (null != object) {
            setupBindViewHolder(object, holder, position, typeIndex);
        }
        holder.itemView.setOnClickListener(view -> {
            if (null != mClickListener && !Utils.isFastClick()) {
                mClickListener.onItemClick(view, holder.getAdapterPosition());
            }
        });
        holder.itemView.setOnLongClickListener(view -> {
            if (null == mLongClickListener) {
                return false;
            }
            mLongClickListener.onItemLongClick(view, holder.getAdapterPosition());
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return (mData == null) ? 0 : mData.size();
    }

    public T getItem(int position) {
        return  (mData == null) ? null : mData.get(position);
    }

    public void add(T data) {
        mData.add(data);
    }

    public void addAll(Collection<T> collection) {
        mData.addAll(collection);
    }

    public boolean remove(T data) {
        int position = mData.indexOf(data);
        if (position != -1) {
            remove(position);
            return true;
        }
        return false;
    }

    public void remove(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

    public boolean contains(T data) {
        return mData.contains(data);
    }

    public void reverse() {
        Collections.reverse(mData);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mLongClickListener = onItemLongClickListener;
    }

    /**
     * @param parent
     * @return
     */
    protected abstract Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex);

    /**
     * viewHold中的view元素设置 setText/setImageDrawable等
     *
     * @param object
     * @param position
     * @param viewHolder
     * @param typeIndex
     */
    protected abstract void setupBindViewHolder(T object, Object viewHolder, int position, int typeIndex);

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    protected static class BaseViewHolder extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }

}
